﻿$(document).keydown(function (e) {
    if (e.keyCode == 13)
        changePassword();
});

$("#_confirmBtn").click(function () {
    changePassword();
});
let opting = false;
function changePassword() {
    if (opting)
        return;
    var _arr = $.cookie('reqConfItem').split(',');
    if (_arr.length !== 2) {
        showTips('[serverError]');
        return;
    }
    var _appId = _arr[0];
    


    let _account = $.trim($("#_account").val());
    let _oldPassword = $("#_oldPassword").val();
    let _newPassword = $("#_newPassword").val();
    if (!_account) {
        $("#_account").parent().parent().addClass('errorInput');
        showTips('[enterAccount]');
        return;
    }
    $("#_account").parent().parent().removeClass('errorInput');
    if (!_oldPassword) {
        $("#_oldPassword").parent().parent().addClass('errorInput');
        showTips('[enterOldPassword]');
        return;
    }
    $("#_oldPassword").parent().parent().removeClass('errorInput');
    if (!_newPassword) {
        $("#_newPassword").parent().parent().addClass('errorInput');
        showTips('[enterNewPassword]');
        return;
    }
    $("#_newPassword").parent().parent().removeClass('errorInput');
    if (_newPassword.length < 10) {
        $("#_newPassword").parent().parent().addClass('errorInput');
        showTips('[shortPassword]');
        return;
    }
    $("#_newPassword").parent().parent().removeClass('errorInput');
    $("#_confirmBtn div").html($("#_confirmBtn div").html()+'...');
    _account = decodeAndMixStr(_account, 1);
    _oldPassword = decodeAndMixStr(_oldPassword, 2);
    _newPassword = decodeAndMixStr(_newPassword, 2);
    var token = { appId: _appId, device_name: 'testPc', loginUserCode: _account, timeStamp: '', language: language, loginSysCode: 'GeneralLogin' }
    var b = new Base64();
    var encdeToken = b.encode(encodeURI(JSON.stringify(token)));
    var signData = { appId: token.appId, device_name: token.device_name, loginUserCode: token.loginUserCode, timeStamp: token.timeStamp, account: _account, oldPassword: _oldPassword,newPassword:_newPassword, language: token.language, loginSysCode: token.loginSysCode };
    var sign = createSign(signData);
    opting = true;
    $.ajax({
        url: apiUrl + '/GeneralLogin/Reset_Password',
        type: 'post',
        data: JSON.stringify({ oldPassword: _oldPassword, newPassword: _newPassword, account: _account, language: language, sign: sign }),
        headers: { token: encdeToken },
        dataType: 'json',
        success: function (r) {
            opting = false;
            if (r.error === 0) {
                let _str = '';
                let _result = '';
                switch (language) {
                    case 'zh':
                        _str = '即将跳转';
                        _result = '修改成功';
                        break;
                    case 'zf':
                        _str = '即將跳轉';
                        _result = '修改成功';
                        break;
                    case 'en':
                        _str = 'About to jump in ';
                        _result = 'Success';
                        break;
                }
                $('#_replaceContent').html('<img src="asset/image/icon_succeed.png" style="margin-top:30px;"/><div style="width:100%;font-size:18px;font-weight:800;color:#ffffff;text-alaign:center;margin-top:10px;letter-spacing:1px;">' + _result + '</div>');
                let timer = 5;
                setInterval(function () {
                    if (timer == 0)
                        location.href = 'login.html';
                    $('#_confirmBtn div').html(_str + timer + 's...');
                    timer -= 1;
                }, 1000);
            }
            else {
                showTips(r.message);
                niceAlert(2,r.message)
		$("#_confirmBtn div").html($("#_confirmBtn div").html().replace('...',''));
            }
            $("#_confirmBtn").removeAttr('disabled');
        }
    });
}
